package com.leetCode.zero;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Problem06 {
	public static void main(String[] args) throws Exception {
		Solution s = getInstance();
		HashMap<String, String> hm = new HashMap<>();
//		hm.put("PAYPALISHIRING", "PAYPALISHIRING");
//		hm.put("PAYPALISHIRING", "PAHNAPLSIIGYIR");
//		Set<Entry<String,String>> entrySet = hm.entrySet();
//		for (Entry<String, String> entry : entrySet) {
//			String key = entry.getKey();
//			String value = entry.getValue();
//			if(value.equals(s.convert(key,3))){
//				System.out.println(key+":"+s.convert(key,3)+"===>"+true);
//			}else{
//				System.out.println(key+":"+s.convert(key,3)+"===>"+false);
//			}
//		}
		String k = "AB";
		System.out.println(k+":"+s.convert(k,1));
	}

	class Solution {
		public String convert(String s, int numRows) {
			if(numRows == 1){
				return s;
			}
			char[] charArray = s.toCharArray();
			StringBuffer[] k = new StringBuffer[numRows];
			int count = 0,length = charArray.length;
			for (int i = 0; i < numRows; i++) {
				k[i] = new StringBuffer();
			}
			int temp = 0;
			int order = -1;
			for (int i = 0; i < length; i++) {
//				System.out.println(temp);
				k[temp].append(charArray[i]);
				if(temp == 0 || temp == (numRows-1)){
					order = order*-1;
				}
				temp += order;
			}
			StringBuffer reslut = new StringBuffer();
			for (StringBuffer stringBuffer : k) {
				reslut.append(stringBuffer);
			}
			return reslut.toString();
		}
	}

	static Solution getInstance() throws Exception {
		String className = Thread.currentThread().getStackTrace()[1].getClassName();
		Class<?> forName = Class.forName(className);
		Method method = forName.getMethod("newS", null);
		Solution solution = (Solution) method.invoke(forName.newInstance(), null);
		return solution;
	}

	public Solution newS() {
		return new Solution();
	}
}
